﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using DomainModel.Concrete;
using DomainModel.Entities;
using System.IO;
using System.Web.Security;

namespace WebUI.Controllers
{
    [Authorize]
    public class ArtistController : Controller
    {
        #region Private Region

        private ActionResult HandleMyError(Exception ex)
        {
            var modelErr = new ErrorHandling(ex);
            return View("Error", modelErr);
        }
        private bool ValidateImageFile(HttpPostedFileBase pictureFile)
        {
            if (pictureFile == null || pictureFile.ContentLength == 0)
            {
                return true;
            }
            if (pictureFile.ContentType == "image/jpeg")
            {
                return true;
            }
            if (pictureFile.ContentType == "image/jpg")
            {
                return true;
            }
            if (pictureFile.ContentType == "image/png")
            {
                return true;
            }
            if (pictureFile.ContentType == "image/bmp")
            {
                return true;
            }
            if (pictureFile.ContentType == "image/gif")
            {
                return true;
            }

            return false;
        }
        #endregion


        SqlArtistRepository _artistRepository;
        SqlFileHostRepository _fileHostRepository;

        public ArtistController()
        {
            _artistRepository = new SqlArtistRepository();
            _fileHostRepository = new SqlFileHostRepository();
        }
        [Authorize]
        public ActionResult Index()
        {
            string strUserName = Membership.GetUser().UserName;
            return View(_artistRepository.GetAllByUserName(strUserName));
        }

        public ActionResult Details(int id)
        {
            ViewData["ArtistID"] = id;
            return View(_artistRepository.GetById(id));
        }

        public ActionResult Create()
        {
            return View(new Artist());
        }

        [HttpPost]
        public ActionResult Create(Artist artist, HttpPostedFileBase pictureFile)
        {
            try
            {
                if (!ValidateImageFile(pictureFile))
                {
                    return View(artist);
                }

                artist.FileHostID = _fileHostRepository.GetActive().FileHostID;
                artist.UserName = Membership.GetUser().UserName;
                artist.LastUpdate = DateTime.Now;
                artist.IsApproved = false;
                artist.HitCount = 0;

                _artistRepository.Insert(artist);
                string _strPictureFileName = artist.FileHostID.ToString() + "_" + artist.ArtistID.ToString();

                if (pictureFile != null && pictureFile.ContentLength > 0)
                {
                    PictureController pictureController = new PictureController();

                    Picture pictureLarge = new Picture();
                    pictureLarge.PictureCaption = "Large-Image-of-" + artist.ArtistName;
                    pictureLarge.PictureFileName = _strPictureFileName + "_Large_Artist";
                    artist.LargePictureID = pictureController.SavePictureFile(pictureLarge, pictureFile, 320, 460);

                    Picture pictureSmall = new Picture();
                    pictureSmall.PictureCaption = "Small-Image-of-" + artist.ArtistName;
                    pictureSmall.PictureFileName = _strPictureFileName + "_Small_Artist";
                    artist.SmallPictureID = pictureController.SavePictureFile(pictureSmall, pictureFile, 80, 80);

                    _artistRepository.Update(artist);
                }

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return HandleMyError(ex);
                //var modelErr = new ErrorHandling(ex);
                //return View("Error", modelErr);
            }
        }

        public ActionResult Edit(int id)
        {
            ViewData["referringUrl"] = Request.UrlReferrer;
            return View(_artistRepository.GetById(id));
        }

        [HttpPost]
        public ActionResult Edit(int id, HttpPostedFileBase pictureFile, FormCollection collection, string referringUrl)
        {
            try
            {
                Artist artist = _artistRepository.GetById(id);
                TryUpdateModel(artist, null, null, null, collection);
                string _strPictureFileName = artist.FileHostID.ToString() + "_" + artist.ArtistID.ToString();

                if (pictureFile != null && pictureFile.ContentLength > 0)
                {
                    PictureController pictureController = new PictureController();
                    if (artist.LargePicture == null)
                    {
                        artist.LargePicture = new Picture();
                        artist.LargePicture.PictureCaption = "Large-Image-of- " + artist.ArtistName;
                        artist.LargePicture.PictureFileName = _strPictureFileName + "_Large";
                    }
                    artist.LargePictureID = pictureController.SavePictureFile(artist.LargePicture, pictureFile, 320, 460);

                    if (artist.SmallPicture == null)
                    {
                        artist.SmallPicture = new Picture();
                        artist.SmallPicture.PictureCaption = "Small-Image-of- " + artist.ArtistName;
                        artist.SmallPicture.PictureFileName = _strPictureFileName + "_Small";
                    }
                    artist.SmallPictureID = pictureController.SavePictureFile(artist.SmallPicture, pictureFile, 80, 80);

                }
                _artistRepository.Update(artist);

                if (string.IsNullOrEmpty(referringUrl))
                {
                    return RedirectToAction("Index", "Home");
                    //return RedirectToAction("Index");
                }
                else
                {
                    return Redirect(referringUrl);
                }

            }
            catch (Exception ex)
            {
                var modelErr = new ErrorHandling(ex);
                return View("Error", modelErr);
            }
        }


        public ActionResult Delete(int id)
        {
            return View();
        }


        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                var modelErr = new ErrorHandling(ex);
                return View("Error", modelErr);
            }
        }
    }
}
